package org.budo.oracle.jdbc.driver;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import org.budo.jdbc.driver.AbstractBudoJdbcConnection;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@NoArgsConstructor
public class BudoOracleJdbcConnection extends AbstractBudoJdbcConnection {
    private Connection connection;

    public BudoOracleJdbcConnection(Connection connection) {
        this.connection = connection;
    }

    @Override
    public Statement createStatement() throws SQLException {
        return new BudoOracleJdbcStatement(this);
    }

    @Override
    public PreparedStatement prepareStatement(String sql) throws SQLException {
        log.info("#33 prepareStatement, sql=" + sql + ", this=" + this);

        if ("SET NAMES utf8".equals(sql)) {
            sql = "set echo on";
        }

        if (sql.contains("@@session.auto_increment_increment")) {
            sql = "SELECT 1 FROM dual";
        }

        PreparedStatement preparedStatement = this.connection.prepareStatement(sql);
        return new BudoOracleJdbcPreparedStatement(preparedStatement);
    }

    @Override
    public DatabaseMetaData getMetaData() throws SQLException {
        return this.connection.getMetaData();
    }
}